

 file open fh using .\Results\Table_CCES_NoProb_age.tex, write replace 

 file write fh "{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{7}{c}}" _n ///
	"\toprule \toprule" _n ///
	"& (1) & (2) & (3) & (4)  \\" _n ///
	" Equal opport. to & \multicolumn{3}{c}{No advantages} & \multicolumn{3}{c}{Racial problems} \\ "_n ///
	" & \multicolumn{3}{c}{for Whites} & \multicolumn{3}{c}{uncommon}  \\ "_n ///
	"\cline{2-7}" _n
	
	local model age
	local col = 6

* White*Male
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local var WhiteMale
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "White Male & `b'`star'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star'" 
}
if `i'==`col' {	
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star' \\" _n
	}
	}

* Second write p-values
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh " & (`pval')" 
}
if `i' >1 & `i'<`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval')" 
}
if `i' ==`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval') " _n
}
	}	
	
	
	
	* The commands below complete the document
file write fh  "\\ \midrule" _n  ///
				"Sample &        Under 25   &   26-64 &        Over 65 &        Under 25   &   26-64 &        Over 65\\[0.4ex]" _n 
* Adding mean
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "Mean dep variable      & `mean'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean'" 
}
if `i'==`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean' \\[0.4ex] " _n
}
	}
				
file write fh  	"Individual controls &          $\checkmark$      &   $\checkmark$       & $\checkmark$ & $\checkmark$  & $\checkmark$ & $\checkmark$    \\[0.4ex]" _n
* Adding observations
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "N.obs      & `N'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N'" 
}
if `i'==`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N' \\ \bottomrule \bottomrule " _n
}
	}
	
				
file write fh		"\end{tabular}}" _n 
file close fh

